package com.example.expinterview.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.expinterview.dto.ArticleDetailDto;
import com.example.expinterview.dto.PageDto;
import com.example.expinterview.dto.SearchDto;
import com.example.expinterview.entity.EiArticle;
import com.example.expinterview.entity.ResultBody;
import com.example.expinterview.service.IEiArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @classname: ArticleController
 * @description: TODO
 * @author: kaiser
 * @date: 2021/10/18 22:11
 * @version: 1.0
 */
@RestController
@RequestMapping("/article")
@Api(tags = "面经接口")
public class ArticleController {
    private final IEiArticleService service;

    @Autowired
    public ArticleController(IEiArticleService service) {
        this.service = service;
    }

    @PostMapping("/saveOrUpdate")
    @ApiOperation(value = "创建或更新面经")
    public ResultBody save(@RequestBody EiArticle article) {
        return ResultBody.status(service.publishArticle(article));
    }

    @PostMapping("/getById")
    @ApiOperation(value = "根据id获取面经内容")
    public ResultBody getById(@RequestBody ArticleDetailDto articleDetailDto) {
        return service.getDetailById(articleDetailDto.getId(), articleDetailDto.getUserId(),articleDetailDto.getShareNo());
    }

    @PostMapping("/pageList")
    @ApiOperation(value = "分页查询")
    public ResultBody pageList(@RequestBody PageDto page) {
        return service.selectPageList(page);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除")
    public ResultBody delete(@PathVariable Long id) {
        return ResultBody.status(service.removeById(id));
    }

    @PostMapping("/search")
    @ApiOperation(value = "搜索面经")
    public ResultBody search(@RequestBody SearchDto searchValue) {
        return service.searchList(searchValue);
    }

    @GetMapping("/getByUser/{userId}")
    @ApiOperation(value = "根据用户ID获取面经")
    public ResultBody getByUser(@PathVariable Long userId) {
        return ResultBody.list(service.list(new QueryWrapper<EiArticle>()
                .eq("user_id", userId)
                .orderByDesc("gmt_create")));
    }

    @PostMapping("/shareArticleLog")
    @ApiOperation(value = "记录分享面经日志，分享面经时调用")
    public ResultBody shareArticleLog(@RequestParam Long articleId,@RequestParam Long userId) {
        return ResultBody.success(service.saveShareArticleLog(articleId, userId));
    }
}
